﻿Public Class Acad_Apply_FormV2

    Dim ProvidersCount As Integer = 0
    Dim LoadingDetails As Boolean = False

    Private Sub LoadFilter()
        TheFilter.zShowButtons = False

        Dim p As New List(Of Object)
        p.Clear()
        p.Add("select * from dbo.View_Provider_Filter_Options where [Provider Type] = 'Academy'")
        p.Add("Preparing filter data, please wait...")

        DoSomeWork("LoadBusyDataView", p)

        TheFilter.zDataView = BusyButFine.ReturnedValues(0)

        '  TheFilter.zDataView = LoadDataView("select * from dbo.View_Provider_Filter_Options where [Provider Type] = 'Academy'")

        ProvidersCount = TheFilter.zDataView.Count

        PeriodBox.zConnectionString = StoreConnect
        PeriodBox.zSelectedPeriod_ID = CurrentPeriodID


        LoadtypeBox()

    End Sub

    Private Sub LoadtypeBox()
        If PeriodBox.zSelectedPeriod_ID >= 2013141 Then
            LoadComboBox(TypeBox, "select * from dbo.Acad_Allocation_Template_IDs where template_id >= 5", "Template_ID", "Description")
        Else
            LoadComboBox(TypeBox, "select * from dbo.Acad_Allocation_Template_IDs where Template_ID <= 4", "Template_ID", "Description")
        End If
    End Sub

    Private Sub Acad_Apply_FormV2_Shown(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Shown
        LoadFilter()
        DisableButtons()

    End Sub

    Private Sub EnableButtons()
        Applybutton.Enabled = True
        SelectAllButton.Enabled = True
        UnselectAllButton.Enabled = True
    End Sub

    Private Sub DisableButtons()
        Applybutton.Enabled = False
        SelectAllButton.Enabled = False
        UnselectAllButton.Enabled = False
    End Sub

    Private Sub LoadDetails()

        Dim r As DataRowView
        Dim DV As DataView

        Dim UPINList As New List(Of String)
        UPINList.Clear()

        LoadingDetails = True

        If TheFilter.zFilterCount > 0 Then

            TheFilter.ApplyFilter()

            If TheFilter.zDataView.Count = 0 Then
                TheFilter.ClearFilter()
                LoadingDetails = False
                MessageBox.Show("The current filter will not produce any records, please change the filter options.", "Please change filter options.", MessageBoxButtons.OK, MessageBoxIcon.Information)
                Exit Sub
            End If

            If ProvidersCount <> TheFilter.zDataView.Count Then

                For Each r In TheFilter.zDataView

                    UPINList.Add(r("UPIN"))

                Next

            End If

            TheFilter.ClearFilter()

        End If

        

        Dim p As New List(Of Object)

        p.Clear()
        p.Add(PeriodBox.zSelectedPeriod_ID) 'period id
        p.Add(TypeBox.SelectedValue) 'Type
        p.Add(UPINList) 'UPIN List
        p.Add(1)

        DoSomeWork("AcadAllocationsView", p)

        DV = BusyButFine.ReturnedValues(0)


        If DV.Table.Columns.Count <> 1 Then
            DV.Table.Columns.Add("Selected", GetType(Integer))
        End If

        TheGrid.DataSource = Nothing

        TheGrid.DataSource = DV

        Dim c As DataGridViewColumn

        For Each c In TheGrid.Columns

            Select Case c.Name
                Case "RID"
                    c.Visible = False
                    c.ReadOnly = True
                Case "UPIN"
                    c.Width = 50
                    c.Frozen = True
                Case "Selected"
                    c.ReadOnly = False
                    c.Width = 50
                Case "Provider Name"
                    'c.AutoSizeMode = DataGridViewAutoSizeColumnMode.DisplayedCells
                    'c.DefaultCellStyle.WrapMode = DataGridViewTriState.True
                    c.Width = 250
                    c.Frozen = True
                Case "Local Authority"
                    'c.AutoSizeMode = DataGridViewAutoSizeColumnMode.DisplayedCells
                    'c.DefaultCellStyle.WrapMode = DataGridViewTriState.True
                Case Else
                    c.ReadOnly = True
                    c.DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight
                    c.DefaultCellStyle.Format = "n"

            End Select

        Next


        With TheGrid

            Dim index As Integer

            Dim TickColumn As New DataGridViewCheckBoxColumn

            TickColumn.Name = "Selected"
            TickColumn.DataPropertyName = "Selected"

            TickColumn.ThreeState = False
            TickColumn.TrueValue = 1
            TickColumn.FalseValue = 0

            index = .Columns.IndexOf(.Columns("Selected"))

            If index <> -1 Then
                .Columns.RemoveAt(index)
                .Columns.Insert(index, TickColumn)
                .Columns("Selected").ReadOnly = False
            End If

        End With

        EnableButtons()
        LoadingDetails = False


    End Sub

    Private Sub LoadSummary()

        Dim r As DataRowView
        Dim DV As DataView

        Dim UPINList As New List(Of String)
        UPINList.Clear()

        LoadingDetails = True

        If TheFilter.zFilterCount > 0 Then

            TheFilter.ApplyFilter()

            If TheFilter.zDataView.Count = 0 Then
                TheFilter.ClearFilter()
                LoadingDetails = False
                MessageBox.Show("The current filter will not produce any records, please change the filter options.", "Please change filter options.", MessageBoxButtons.OK, MessageBoxIcon.Information)
                Exit Sub
            End If

            If ProvidersCount <> TheFilter.zDataView.Count Then

                For Each r In TheFilter.zDataView

                    UPINList.Add(r("UPIN"))

                Next

            End If

            TheFilter.ClearFilter()

        End If



        Dim p As New List(Of Object)

        p.Clear()
        p.Add(PeriodBox.zSelectedPeriod_ID) 'period id
        p.Add(TypeBox.SelectedValue) 'Type
        p.Add(UPINList) 'UPIN List
        p.Add(0)

        DoSomeWork("AcadAllocationsView", p)

        DV = BusyButFine.ReturnedValues(0)


        TheGrid.DataSource = Nothing

        TheGrid.DataSource = DV

        Dim c As DataGridViewColumn

        For Each c In TheGrid.Columns

            Select Case c.Name
                Case "RID"
                    c.Visible = False
                    c.ReadOnly = True
                Case "UPIN"
                    c.Width = 50
                    c.Frozen = True
                Case "Selected"
                    c.ReadOnly = False
                    c.Width = 50
                Case "Provider Name"
                    'c.AutoSizeMode = DataGridViewAutoSizeColumnMode.DisplayedCells
                    'c.DefaultCellStyle.WrapMode = DataGridViewTriState.True
                    c.Width = 250
                    c.Frozen = True
                Case "Local Authority"
                    'c.AutoSizeMode = DataGridViewAutoSizeColumnMode.DisplayedCells
                    'c.DefaultCellStyle.WrapMode = DataGridViewTriState.True
                Case Else
                    c.ReadOnly = True
                    c.DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight
                    c.DefaultCellStyle.Format = "n"

            End Select

        Next


        LoadingDetails = False


    End Sub

    Private Sub ViewButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ViewButton.Click
        LoadDetails()

    End Sub

    Private Sub TheGrid_CellFormatting(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellFormattingEventArgs) Handles TheGrid.CellFormatting
        'If LoadingDetails Then
        '    Me.Cursor = Cursors.WaitCursor

        'End If

    End Sub

    Private Sub TheGrid_RowPostPaint(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewRowPostPaintEventArgs) Handles TheGrid.RowPostPaint
        'If Not LoadingDetails Then
        '    Me.Cursor = Cursors.Default

        'End If
    End Sub

    Private Sub SelectAllButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SelectAllButton.Click
        Dim r As DataRowView

        For Each r In TheGrid.DataSource
            Try
                r("Selected") = 1
            Catch ex As Exception

            End Try


        Next
    End Sub

    Private Sub UnselectAllButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles UnselectAllButton.Click
        Dim r As DataRowView

        For Each r In TheGrid.DataSource
            Try
                r("Selected") = 0
            Catch ex As Exception

            End Try


        Next
    End Sub

    Private Sub ApplyAllocations()

        Dim p As New List(Of Object)
        Dim RIDlist As New List(Of String)
        Dim r As DataRowView

        RIDlist.Clear()

        For Each r In TheGrid.DataSource

            If Not IsDBNull(r("Selected")) Then
                If r("Selected") = 1 Then
                    RIDlist.Add(r("RID"))
                End If
            End If

            
        Next

        If RIDlist.Count = 0 Then Exit Sub

        p.Clear()
        p.Add(PeriodBox.zSelectedPeriod_ID) 'Period
        p.Add(TypeBox.SelectedValue) 'Template Type
        p.Add(RIDlist) 'RID list

        If DoSomeWork("ApplyAcademyAllocations", p, False) = Windows.Forms.DialogResult.OK Then
            p.Clear()
            p.Add(PeriodBox.zSelectedPeriod_ID) 'Period
            p.Add("YPLRE") 'Budget ID
            p.Add(RIDlist) 'RID list

            DoSomeWork("UpdateActivePaymentsBudget", p, False)

            LoadDetails()

            MessageBox.Show("The allocations have been applied succesfully", "Success", MessageBoxButtons.OK, MessageBoxIcon.Information)

        End If

    End Sub

    Private Sub Applybutton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Applybutton.Click
        If MessageBox.Show("Are you sure you want to apply the allocations for the selected providers?", "Are you sure?", MessageBoxButtons.YesNo, MessageBoxIcon.Question) = Windows.Forms.DialogResult.Yes Then
            ApplyAllocations()
        End If

    End Sub

   
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        LoadSummary()

    End Sub

    Private Sub TypeBox_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TypeBox.SelectedIndexChanged
        TheGrid.DataSource = Nothing

    End Sub

    Private Sub PeriodBox_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PeriodBox.SelectedIndexChanged
        TheGrid.DataSource = Nothing
        LoadtypeBox()

    End Sub

   
End Class